<html>
<head><meta charset="utf-8"><title>LinkedList vs Stacked Borrows · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html">LinkedList vs Stacked Borrows</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="163637192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/163637192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#163637192">(Apr 18 2019 at 10:02)</a>:</h4>
<p>I found a stacked borrows violation in LinkedList: <a href="https://github.com/rust-lang/rust/pull/60072" target="_blank" title="https://github.com/rust-lang/rust/pull/60072">https://github.com/rust-lang/rust/pull/60072</a><br>
Is that good news because it's one bug less or bad news because not even libstd can get the aliasing right? I don't know.^^</p>



<a name="163637257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/163637257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#163637257">(Apr 18 2019 at 10:03)</a>:</h4>
<p>or maybe this is a data point for "mutable reference should only become unique once written to the first time"? That would however severely reduce the amount of optimizations we can do.</p>



<a name="163651116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/163651116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#163651116">(Apr 18 2019 at 13:27)</a>:</h4>
<p>here's another Stacked Borrows violation that's also about a mutable reference that got created but never used: <a href="https://github.com/SimonSapin/rust-typed-arena/issues/26" target="_blank" title="https://github.com/SimonSapin/rust-typed-arena/issues/26">https://github.com/SimonSapin/rust-typed-arena/issues/26</a></p>



<a name="163651257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/163651257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#163651257">(Apr 18 2019 at 13:29)</a>:</h4>
<p>This mutable reference <em>does</em> however get passed to a function, namely <code>as_mut_ptr</code>. So even if we wanted to say that only writes and passing-to-a-function actually make an <code>&amp;mut</code> unique (which could save some optimizations), there'd still be a bug here.</p>



<a name="164189820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164189820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164189820">(Apr 25 2019 at 17:12)</a>:</h4>
<p>I'm new here, but it seems like these cases are all "dead code/ability" cases, i.e. requesting a feature or doing a thing that is then never used, right?</p>



<a name="164189845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164189845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164189845">(Apr 25 2019 at 17:12)</a>:</h4>
<p>Perhaps we can remove those before doing optimizations?</p>



<a name="164189954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164189954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164189954">(Apr 25 2019 at 17:14)</a>:</h4>
<p>well the reference does get used, but only for some fields of the struct</p>



<a name="164189966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164189966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164189966">(Apr 25 2019 at 17:14)</a>:</h4>
<p>so you cannot really remove the reference entirely</p>



<a name="164190019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164190019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164190019">(Apr 25 2019 at 17:16)</a>:</h4>
<p>Right, but aren't you just downgrading them in the fix?</p>



<a name="164190076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164190076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164190076">(Apr 25 2019 at 17:16)</a>:</h4>
<p>I guess I'm not clear on exactly what's wrong, sorry.</p>



<a name="164242750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164242750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164242750">(Apr 26 2019 at 08:33)</a>:</h4>
<p>Im replacing referenced by raw pointers</p>



<a name="164576253"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164576253" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> JP Sugarbroad <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164576253">(Apr 30 2019 at 20:59)</a>:</h4>
<p>Ah, ok, taking advantage of the fact that raw pointers don't constrain things as much as a reference does. But nevertheless, you mentioned "mutable reference that got created but never used" -- can that be detected and removed before analysis?</p>



<a name="164612091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164612091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164612091">(May 01 2019 at 09:42)</a>:</h4>
<p>I responded to that above:</p>
<blockquote>
<p>well the reference does get used, but only for some fields of the struct<br>
so you cannot really remove the reference entirely</p>
</blockquote>



<a name="164612106"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164612106" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164612106">(May 01 2019 at 09:42)</a>:</h4>
<p>also, when defining a semantics and e.g. implementing it in Miri, it's really hard to say "oh and if this value we just created never gets looked at, then these side-effects also dont happen"</p>



<a name="164612123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164612123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164612123">(May 01 2019 at 09:43)</a>:</h4>
<p>I wouldn't know how to define that without getting into contradictions. Miri would have to predict the future or something?</p>



<a name="164612139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList%20vs%20Stacked%20Borrows/near/164612139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/LinkedList.20vs.20Stacked.20Borrows.html#164612139">(May 01 2019 at 09:43)</a>:</h4>
<p>You could to a source-to-source translation before defining the semantics, bu that would be a very weird mix of static and dynamic approaches for the semantics. Probably possible, but very hard to analyze and reason about, which would also be a problem.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>